// subpkg_worker/laboratorySheets/laboratorySheets.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    elderId:'',
    show: false,
    fileList: [],
    laboratoryUrl:'',
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date)
    var year = date.getFullYear()
    var month = date.getMonth() + 1 < 10? `0${date.getMonth() + 1}` : date.getMonth() + 1
    var day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()
    console.log(day)
    return `${year}-${month}-${day}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  afterRead(event){
    var fileList = []
    const { file } = event.detail
    fileList.push(file)
    console.log(event.detail.file.tempFilePath)
    this.setData({
      laboratoryUrl:event.detail.file.tempFilePath,
      fileList:fileList
    })

  },
  submit(){
    if(this.data.laboratoryUrl===''||this.data.date===''){
      Toast('请完善信息后再保存')
      return
    }
    const that = this
    wx.uploadFile({
      url: 'https://zhixiao.site:8080/health/laboratory/upload', // 仅为示例，非真实的接口地址
      filePath: this.data.laboratoryUrl,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        // 上传完成需要更新 fileList
        var url = res.data.slice(32,res.data.length-3)
        console.log(url)
        that.setData({
          laboratoryUrl:url
        })
      },
      complete:(res)=>{
        var url = res.data.slice(32,res.data.length-3)
        wx.request({
          url: 'https://zhixiao.site:8080/health/laboratory/add',
          data:{
            elderId:this.data.elderId,
            laboratoryUrl:url,
            testTime:that.data.date
          },
          success:(res)=>{
            console.log(res)
            Toast('保存成功!')
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      elderId:options.elderId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})